package luit.mmt02.activity;

import java.util.ArrayList;
import java.util.List;

import luit.mmt02.R;
import luit.mmt02.adapter.attach_adapter;
import luit.mmt02.doituong.attach;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;

public class attach_send_manager extends Activity
{
	final static int REQUEST_CHUOI_ATTACH_SEND = 99;
	final static String KEY_CHUOI_ATTACH_SEND = "chuoi_attach_send";
	ListView listview;
	ArrayAdapter<attach>adapter;
	List<attach>list;
	int requestcode = 1;
	
	//
	String tag = "attach_send_manager";
	String str_att;  // chuoi ds cac ng nhan gui qua new mail
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.attach_send_manager);
		listview = (ListView) findViewById(R.id.lv_attachsend);
		list = new ArrayList<attach>();
		str_att = getIntent().getStringExtra(KEY_CHUOI_ATTACH_SEND);
		Log.v(tag, "nhan duoc chuoi : " + str_att);
		if(!str_att.equals(""))
		{
			String []arr_att = str_att.split(";");
			for(String str_1_att:arr_att)
			{
				list.add(new attach(str_1_att, true));
			}
		}
		adapter = new attach_adapter(this, list);
		listview.setAdapter(adapter);
		
		//
//		setResult(REQUEST_CHUOI_ATTACH_SEND);
//		finish();
	}
	
	public void img_onClick(View v)
	{
		ImageButton img = (ImageButton)v;
		switch (img.getId()) {
		case R.id.img_att_send_backA:
			finish();
			break;
		case R.id.img_att_send_menu:
			openOptionsMenu();
			break;

		default:
			break;
		}
	}
	
	public void btn_onClick(View v)
	{
		Button btn = (Button)v;
		switch (btn.getId()) {
		case R.id.btn_att_send_OK:
//			Intent i_newmail = new Intent(this, newmailActivity.class);
			Intent i_newmail = new Intent();
//			i_newmail.putExtra(KEY_CHUOI_ATTACH_SEND, str_send_to_newmail());
			Log.v(tag, str_att);
			str_att = str_send_to_newmail();
			Log.v(tag, str_att);
			i_newmail.putExtra(KEY_CHUOI_ATTACH_SEND, str_att);
//			startActivity(i_newmail);
			this.setResult(REQUEST_CHUOI_ATTACH_SEND, i_newmail);
			Log.v("send manager ", str_send_to_newmail());
			finish();
			break;
		case R.id.btn_att_send_add:
			startActivityForResult(new Intent(this, FileDialog_OpenActivity.class), requestcode);
			break;
		default:
			break;
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		switch (resultCode) {
		case RESULT_OK:
			String path = data.getData().toString();
//			listatt.add(path); // them ds dinh kem vao list att de gui
//			adapter.add( path);
			str_att += path;
			str_att += ";";
			Log.v(tag, "sau khi chon file : str_att : " +str_att);
			list.add(new attach(path, true));
			adapter.notifyDataSetChanged();
			break;
		default:
			break;
		}
	}
	
	public String str_send_to_newmail()
	{
		String str = "";
		for(int i = 0; i < list.size(); i++)
		{
			if(list.get(i).isCheck())
			{
				str += list.get(i).getStr_attach();
				str += ";";
			}
		}
		Log.v("chuoi att gui qua new mail", str);
		return str;
	}
}
